package com.wswy.wzcx.ui.card

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.support.constraint.ConstraintLayout
import android.support.v4.content.ContextCompat
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.SizeUtils
import com.makeramen.roundedimageview.RoundedDrawable
import com.makeramen.roundedimageview.RoundedImageView
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.model.license.VehicleLicenseModel
import com.wswy.wzcx.widget.drawable.BaseDrawable
import kotlinx.android.synthetic.main.item_license_card.view.*

class LicenseCardLayout : ConstraintLayout,View.OnClickListener {



    constructor(context: Context) : this(context, null)

    constructor(context: Context, attribute: AttributeSet?) : this(context, attribute, 0)

    constructor(context: Context, attribute: AttributeSet?, defStyle: Int) : super(context, attribute, defStyle) {

        View.inflate(context, R.layout.item_license_card,this)

        val leftD = ColorDrawable(ContextCompat.getColor(context,R.color.white50)).apply {
            setBounds(0,0,SizeUtils.dp2px(35f),SizeUtils.dp2px(35f))
        }

        img_license_front.hierarchy?.setPlaceholderImage(DefLicenseBgDrawable("中华人民共和国机动车行驶证",leftD,null))
        img_license_back.hierarchy?.setPlaceholderImage(DefLicenseBgDrawable("中华人民共和国机动车行驶证",null,leftD))


        layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)

        //tv_delete.setOnClickListener(this)
    }

    fun bindData(data:VehicleLicenseModel?){
        tv_car_no.text = data?.carNo

    }


    fun deleteView() = tv_delete


    override fun onClick(v: View?) {

    }

    private class DefLicenseBgDrawable(private val text:String,
                                       private val bottomLeftDrawable:Drawable?,
                                       private val bottomRightDrawable:Drawable?) : BaseDrawable(){

        private val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
            textSize = SizeUtils.sp2px(10f).toFloat()
            color = ContextCompat.getColor(AppContext.getContext(),R.color.white70)
        }

        private val bgColor = Color.parseColor("#809FFF")

        val margin = SizeUtils.dp2px(10f).toFloat()

        private val centerDrawable = ContextCompat.getDrawable(AppContext.getContext(),R.drawable.ic_addtonew_white)?.apply {
            setBounds(0,0,SizeUtils.dp2px(32f),SizeUtils.dp2px(32f))
        }

        private val rectF = RectF()

        override fun draw(canvas: Canvas) {

            paint.color = bgColor
            paint.style = Paint.Style.FILL
            val r = SizeUtils.dp2px(6f).toFloat()

            rectF.set(rect)

            canvas.drawRoundRect(rectF,r,r,paint)

            val tW = textPaint.measureText(text)


            canvas.drawText(text, (rect.width() - tW) / 2f, margin*2, textPaint)

            centerDrawable?.let {
                canvas.save()
                canvas.translate((rect.width() - it.bounds.width())/2f,(rect.height() - it.bounds.height())/2f)
                it.draw(canvas)
                canvas.restore()
            }

            bottomLeftDrawable?.let {
                canvas.save()
                canvas.translate(margin,rect.height() - it.bounds.height()-margin)
                it.draw(canvas)
                canvas.restore()
            }


            bottomRightDrawable?.let {
                canvas.save()
                canvas.translate(rect.width() - it.bounds.width()-margin,rect.height()-it.bounds.height()-margin)
                it.draw(canvas)
                canvas.restore()
            }

        }

    }


}